package com.example.javaee_blog.controller;

import com.example.javaee_blog.constants.Constant;
import com.example.javaee_blog.model.BlogInfo;
import com.example.javaee_blog.service.BlogService;
import com.example.javaee_blog.utils.JwtUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogService blogService;

    @RequestMapping("/getList")
    List<BlogInfo> queryAll() {
        List<BlogInfo> res = blogService.queryAll();
        List<BlogInfo> ans = new ArrayList<>();
        for (BlogInfo blogInfo : res) {
            String s = blogInfo.getContent();
            //System.out.println(s.startsWith("##"));
            if (s.startsWith("##")) {
                s = s.substring(12);
                //System.out.println(s);
            }
            if (s.length() > 50)
                blogInfo.setContent(s.substring(0, 50) + "...");
            else
                blogInfo.setContent(s.substring(0));
            //System.out.println(blogInfo.getContent());
            ans.add(blogInfo);
        }
        return ans;
    }

    @RequestMapping("/getBlogDetail")
    BlogInfo queryBlogDetail(Integer blogId, HttpServletRequest request) {
        BlogInfo blogInfo = blogService.queryBlogDetail(blogId);
        String user_token = request.getHeader(Constant.USER_TOKEN_HANDLER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if (userId != null && userId == blogInfo.getUserId())
            blogInfo.setIsLoginUser(true);
        return blogInfo;
    }

    @RequestMapping("/add")
    Boolean publishBlog(String title, String content, HttpServletRequest request) {
        if (!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            log.error("title or content 为空,title:" + title + "content" + content);
            return false;
        }
        String user_token = request.getHeader(Constant.USER_TOKEN_HANDLER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if (userId == null || userId < 1) {
            log.error("userId:" + userId);
            return false;
        }
        BlogInfo blogInfo = new BlogInfo(title, content, userId);
        Integer res = blogService.publishBlog(blogInfo);
        if (res < 1)
            return false;
        return true;
    }
    @RequestMapping("/update")
    public Boolean update(Integer blogId, String title, String content) {
        if (blogId < 1 || !StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        Integer res = blogService.update(blogInfo);
        if(res < 1) return false;
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer res = blogService.update(blogInfo);
        if(res < 1) return false;
        return true;
    }
}
